package com.zz.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zz.common.R;
import com.zz.entity.Activity;
import com.zz.entity.ApplicationAuditing;
import com.zz.entity.Attended;
import com.zz.entity.User;
import com.zz.mapper.ActivityMapper;
import com.zz.mapper.AttendedMapper;
import com.zz.mapper.UserMapper;
import com.zz.service.AttendedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class AttendedServiceImpl  extends ServiceImpl<AttendedMapper, Attended> implements AttendedService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private AttendedMapper attendedMapper;

    /**
     * 用户查看已参加未举办活动
     * */
    @Override
    public R<List<Activity>> attendedUnstart(HttpServletRequest request) {
        String userName = (String) request.getSession().getAttribute("user");
        // 查询用户信息
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 获取用户学号
        int userId = user.getId();

        // 查询已参加活动表
        LambdaQueryWrapper<Attended> attendedWrapper = new LambdaQueryWrapper<>();
        attendedWrapper.eq(Attended::getUserId, userId);
        List<Attended> attendeds = attendedMapper.selectList(attendedWrapper);

        // 创建活动id集合
        Set<Integer> activityIds = new HashSet<>();
        // 遍历所有审核通过的活动报名表
        for (Attended attended : attendeds) {
            activityIds.add(attended.getActivityId());
        }

        // 查询所有活动详细信息
        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
        // 获取当前时间
        Date date = new Date(System.currentTimeMillis());
        activityWrapper.in(Activity::getId, activityIds).ge(Activity::getTime, date);
        List<Activity> activities = activityMapper.selectList(activityWrapper);

        if(activities.isEmpty()){
            return R.error("用户没有已参加但并未举办的活动！");
        }

        int size = activities.size();
        return R.success(activities).add("total", size);
    }

    @Override
    public R<List<Activity>> attendedEnd(HttpServletRequest request) {
        String userName = (String) request.getSession().getAttribute("user");
        // 查询用户信息
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 获取用户学号
        int userId = user.getId();

        // 查询已参加活动表
        LambdaQueryWrapper<Attended> attendedWrapper = new LambdaQueryWrapper<>();
        attendedWrapper.eq(Attended::getUserId, userId);
        List<Attended> attendeds = attendedMapper.selectList(attendedWrapper);

        // 创建活动id集合
        Set<Integer> activityIds = new HashSet<>();
        // 遍历所有审核通过的活动报名表
        for (Attended attended : attendeds) {
            activityIds.add(attended.getActivityId());
        }

        // 查询所有活动详细信息
        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
        // 获取当前时间
        Date date = new Date(System.currentTimeMillis());
        // 小于等于
        activityWrapper.in(Activity::getId, activityIds).le(Activity::getTime, date);
        List<Activity> activities = activityMapper.selectList(activityWrapper);

        if(activities.isEmpty()){
            return R.error("用户没有已参加并且已结束的活动！");
        }

        int size = activities.size();
        return R.success(activities).add("total", size);
    }

    /**
     * 用户查看已参加已结束活动(没有填写调查问卷)
     * */
    @Override
    public R<List<Activity>> attendedEndUnQ(HttpServletRequest request) {
        String userName = (String) request.getSession().getAttribute("user");
        // 查询用户信息
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName, userName);
        User user = userMapper.selectOne(userWrapper);

        // 获取用户学号
        int userId = user.getId();

        // 查询已参加活动表
        LambdaQueryWrapper<Attended> attendedWrapper = new LambdaQueryWrapper<>();
        attendedWrapper.eq(Attended::getIfInvestigated, 0);
        List<Attended> attendeds = attendedMapper.selectList(attendedWrapper);

        // 创建活动id集合
        Set<Integer> activityIds = new HashSet<>();
        // 遍历所有已参加活动表
        for (Attended attended : attendeds) {
            activityIds.add(attended.getActivityId());
        }

        // 查询所有活动详细信息
        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
        // 获取当前时间
        Date date = new Date(System.currentTimeMillis());
        // 小于等于
        activityWrapper.in(Activity::getId, activityIds).le(Activity::getTime, date);
        List<Activity> activities = activityMapper.selectList(activityWrapper);

        if(activities.isEmpty()){
            return R.error("用户没有还未填写调查问卷(已参加并且已结束)的活动！");
        }

        int size = activities.size();
        return R.success(activities).add("total", size);
    }
}
